package com.link.base.base.product.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.link.base.base.product.model.ProdViewHistory;
import com.link.base.base.product.service.ProdViewHistoryService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;


import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author KuangRuifeng
 * created 2018-11-08
 * comments 产品浏览历史记录控制器
 */
@Controller
@RequestMapping("/link/prodViewHistory")
public class ProdViewHistoryController extends BasicController<ProdViewHistory> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private ProdViewHistoryService prodViewHistoryService;

    @Override
    public BasicService<ProdViewHistory> getBasicService() throws Exception {
        return prodViewHistoryService;
    }

    /**
     * 根据客户id和产品id添加或更新浏览记录
     * @author KuangRuifeng
     * @date 2018年11月9日
     */
    @RequestMapping(value = "/upsertViewHis")
    @ResponseBody
    public Map<String, Object> upsertViewHis(@RequestBody ProdViewHistory pvh, HttpSession session,
                                             HttpServletRequest request, HttpServletResponse response) {
        LCLogger.info().withMessageKey("upsertViewHis")
                .withMessage("添加或者修改一条浏览记录").flush();
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            ProdViewHistory newRow = prodViewHistoryService.upsertViewHis(pvh);
            result.put("newRow", newRow);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }
}
